using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace TKH.BizTalk.Diagnostics
{
    /// <summary>
    /// Logger used to log events
    /// </summary>
    /// <remarks></remarks>
    public class LogSource
    {
        static Dictionary<string, System.Diagnostics.TraceSource> sources = new Dictionary<string, System.Diagnostics.TraceSource>();
        static object sourceLock = new object();
        
        /// <summary>
        /// Writes trace data to listeners in <see cref="System.Diagnostics.TraceListener"/> collection 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="biztalkEvent"></param>
        protected static void TraceData(string source, BizTalkEvent biztalkEvent)
        {
            TraceSourceFactory(source).TraceData(biztalkEvent.Severity, biztalkEvent.EventId, biztalkEvent);
        }
        /// <summary>
        /// Writes trace data to listeners in <see cref="System.Diagnostics.TraceListener"/> collection 
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="source"></param>
        /// <param name="eventId"></param>
        /// <param name="data"></param>
        protected static void TraceData(TraceEventType eventType, string source, int eventId, params object[] data)
        {
            TraceSourceFactory(source).TraceData(eventType, eventId, data);
        }
        /// <summary>
        /// Writes trace event to listeners in <see cref="System.Diagnostics.TraceListener"/> collection 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="eventId"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        protected static void TraceEvent(string source, int eventId, string message, params object[] args)
        {
            TraceEvent(System.Diagnostics.TraceEventType.Information, source, eventId, message, args);
        }
        /// <summary>
        /// Writes trace event to listeners in <see cref="System.Diagnostics.TraceListener"/> collection 
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="source"></param>
        /// <param name="eventId"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        protected static void TraceEvent(TraceEventType eventType, string source, int eventId, string message, params object[] args)
        {
            TraceSourceFactory(source).TraceEvent(eventType, eventId, message, args);
        }
        /// <summary>
        /// Writes trace transfer to listeners in <see cref="System.Diagnostics.TraceListener"/> collection 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="eventId"></param>
        /// <param name="message"></param>
        /// <param name="relatedActivityId"></param>
        protected static void TraceTransfer(string source, int eventId, string message, Guid relatedActivityId)
        {
            TraceSourceFactory(source).TraceTransfer(eventId, message, relatedActivityId);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static System.Diagnostics.TraceSource TraceSourceFactory(string source)
        {
            if (!sources.ContainsKey(source))
            {
                lock (sourceLock)
                {
                    if (!sources.ContainsKey(source))
                    {
                        sources.Add(source, new System.Diagnostics.TraceSource(source));
                    }
                }
            }
            return sources[source];
        }
    }
}
